# - Try to find the code signing command for Emgu
#
# defines
#
# EMGU_SIGN_EXECUTABLE - Path to the 'EmguSign.bat' command
# copyright (c) 2009 - 2021 Canming Huang support@emgu.com


SET(EMGU_SIGN_FOUND FALSE)

FIND_PROGRAM (OPENVSIXSIGNTOOL_EXECUTABLE 
NAMES OpenVsixSignTool 
PATHS
"C:/CodeSignTool"
CMAKE_FIND_ROOT_PATH_BOTH
)
IF (OPENVSIXSIGNTOOL_EXECUTABLE)
  SET (OPENVSIXSIGNTOOL_FOUND TRUE)
ELSE()
  SET (OPENVSIXSIGNTOOL_FOUND FALSE)
ENDIF()  
MESSAGE(STATUS "OPENVSIXSIGNTOOL_EXECUTABLE : ${OPENVSIXSIGNTOOL_EXECUTABLE}")


FIND_PROGRAM (EMGU_SIGN_EXECUTABLE 
NAMES EmguSign.bat 
PATHS
"C:/CodeSignTool"
CMAKE_FIND_ROOT_PATH_BOTH
)

FIND_PROGRAM (EMGU_NUGET_SIGN_EXECUTABLE 
NAMES EmguSignNuget.bat 
PATHS
"C:/CodeSignTool"
CMAKE_FIND_ROOT_PATH_BOTH
)

FIND_PROGRAM (EMGU_VSIX_SIGN_EXECUTABLE 
NAMES EmguSignVSIX.bat 
PATHS
"C:/CodeSignTool"
CMAKE_FIND_ROOT_PATH_BOTH
)

IF(EMGU_SIGN_EXECUTABLE)
  MESSAGE( STATUS "FOUND EMGU_SIGN_EXECUTABLE: ${EMGU_SIGN_EXECUTABLE}" )
  SET(EMGU_SIGN_FOUND TRUE)
ENDIF()

IF(EMGU_NUGET_SIGN_EXECUTABLE)
  MESSAGE( STATUS "FOUND EMGU_NUGET_SIGN_EXECUTABLE: ${EMGU_NUGET_SIGN_EXECUTABLE}" )
  SET(EMGU_NUGET_SIGN_FOUND TRUE)
ENDIF()

IF(EMGU_VSIX_SIGN_EXECUTABLE)
  MESSAGE( STATUS "FOUND EMGU_VSIX_SIGN_EXECUTABLE: ${EMGU_VSIX_SIGN_EXECUTABLE}" )
  SET(EMGU_VSIX_SIGN_FOUND TRUE)
ENDIF()

MACRO(EMGU_SIGN_BINARY target file_full_path)
  IF (EMGU_SIGN_FOUND AND SIGNTOOL_FOUND)
    GET_FILENAME_COMPONENT(EMGU_SIGN_WORKING_DIR ${file_full_path} DIRECTORY)
	GET_FILENAME_COMPONENT(EMGU_SIGN_FILE_NAME ${file_full_path} NAME)
    STRING(REGEX REPLACE "/" "\\\\" WIN_BINARY_FULL_PATH "${file_full_path}" )
    SET(SIGNED_BINARY_DIRECTORY "${EMGU_SIGN_WORKING_DIR}/signed")
    STRING(REGEX REPLACE "/" "\\\\" WIN_SIGNED_BINARY_DIRECTORY "${SIGNED_BINARY_DIRECTORY}")
    add_custom_command(TARGET ${target}
      POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E make_directory ${WIN_SIGNED_BINARY_DIRECTORY}
      COMMAND ${EMGU_SIGN_EXECUTABLE} "${WIN_BINARY_FULL_PATH}" "${WIN_SIGNED_BINARY_DIRECTORY}" "${SIGNTOOL_EXECUTABLE}"
      #COMMAND ${CMAKE_COMMAND} -E copy ${SIGNED_BINARY_DIRECTORY}/${EMGU_SIGN_FILE_NAME} ${EMGU_SIGN_WORKING_DIR}
	  COMMAND ${CMAKE_COMMAND} -E copy_directory ${SIGNED_BINARY_DIRECTORY} ${EMGU_SIGN_WORKING_DIR}  # Need this to copy the dlls that may have been overrided in other compile operation.
      WORKING_DIRECTORY "${EMGU_SIGN_WORKING_DIR}"
      COMMENT "Signing ${file_full_path}")
  ENDIF ()
ENDMACRO()

MACRO(EMGU_SIGN_NUGET target file_full_path)
  IF (EMGU_NUGET_SIGN_FOUND)
    GET_FILENAME_COMPONENT(EMGU_SIGN_WORKING_DIR ${file_full_path} DIRECTORY)
	GET_FILENAME_COMPONENT(EMGU_SIGN_FILE_NAME ${file_full_path} NAME)
    STRING(REGEX REPLACE "/" "\\\\" WIN_BINARY_FULL_PATH "${file_full_path}" )
    SET(SIGNED_BINARY_DIRECTORY "${EMGU_SIGN_WORKING_DIR}/signed")
    STRING(REGEX REPLACE "/" "\\\\" WIN_SIGNED_BINARY_DIRECTORY "${SIGNED_BINARY_DIRECTORY}")
    add_custom_command(TARGET ${target}
      POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E make_directory ${SIGNED_BINARY_DIRECTORY}
      COMMAND ${EMGU_NUGET_SIGN_EXECUTABLE} "${WIN_BINARY_FULL_PATH}" "${WIN_SIGNED_BINARY_DIRECTORY}" "${NUGET_EXECUTABLE}"
      #COMMAND ${CMAKE_COMMAND} -E copy ${SIGNED_BINARY_DIRECTORY}/${EMGU_SIGN_FILE_NAME} ${EMGU_SIGN_WORKING_DIR}
	  COMMAND ${CMAKE_COMMAND} -E copy_directory ${SIGNED_BINARY_DIRECTORY} ${EMGU_SIGN_WORKING_DIR}  # Need this to copy the dlls that may have been overrided in other compile operation.
      WORKING_DIRECTORY "${EMGU_SIGN_WORKING_DIR}"
      COMMENT "Signing ${file_full_path}")
  ENDIF ()
ENDMACRO()

MARK_AS_ADVANCED(EMGU_SIGN_EXECUTABLE EMGU_SIGN_FOUND)

